package net.ib.mtalk.ui.fragment;

import net.ib.mtalk.R;
import net.ib.mtalk.database.DatabaseContract.ViewTable;
import net.ib.mtalk.database.helper.InboxDatabaseHelper;
import net.ib.mtalk.ui.HomeActivity;
import net.ib.mtalk.ui.chat.ChatListActivity;
import net.ib.mtalk.ui.chat.ChatListBackgroundActivity;
import net.ib.mtalk.ui.inbox.InboxListActivity;
import net.ib.mtalk.ui.inbox.InboxListCursorAdapter;
import android.content.Intent;
import android.database.Cursor;
import android.database.DataSetObserver;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.CursorAdapter;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemLongClickListener;

/**
 * Inbox ListView Widget
 * 
 * @author happy2v
 *
 */
public class InboxListFragment extends ListFragment implements
		LoaderManager.LoaderCallbacks<Cursor> {
	
	CursorAdapter mAdapter;

	String mCurFilter;

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		setHasOptionsMenu(true);
		
		mAdapter = new InboxListCursorAdapter(getActivity(), null, 0);
		setListAdapter(mAdapter);

		ListView lv = getListView();
		lv.setDivider(getResources().getDrawable(R.drawable.inbox_divider));
		lv.setDividerHeight(1);
		lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		lv.setCacheColorHint(Color.TRANSPARENT);
		lv.setOnItemLongClickListener(new OnItemLongClickListener() {
			// Item Long 클릭 이벤트
			@Override
			public boolean onItemLongClick(AdapterView<?> av, View v,
					int position, long id) {
				int chatType = mAdapter.getCursor().getInt(mAdapter.getCursor().getColumnIndexOrThrow(ViewTable.KEY_CHAT_TYPE));
				int pushType = mAdapter.getCursor().getInt(mAdapter.getCursor().getColumnIndexOrThrow(ViewTable.KEY_VIEW_PUSH_ONOFF));
				int enableType = mAdapter.getCursor().getInt(mAdapter.getCursor().getColumnIndexOrThrow(ViewTable.KEY_VIEW_ENABLE));
				long clientViewUuidMsb = mAdapter.getCursor().getLong(mAdapter.getCursor().getColumnIndex(ViewTable.KEY_CLIENT_VIEW_UUID_MSB));
				long clientViewUuidLsb = mAdapter.getCursor().getLong(mAdapter.getCursor().getColumnIndex(ViewTable.KEY_CLIENT_VIEW_UUID_LSB));
				
				((InboxListActivity) getActivity()).showDialogMenu(chatType, pushType, enableType, clientViewUuidMsb, clientViewUuidLsb);
				
				return true;
			}
		});
		
		getLoaderManager().initLoader(0, null, this);
	}
	
	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		super.onCreateOptionsMenu(menu, inflater);
	}
 
	public boolean onQueryTextChange(String newText) {
		mCurFilter = !TextUtils.isEmpty(newText) ? newText : null;
		getLoaderManager().restartLoader(0, null, this);
		return true;
	}

	// row 클릭시 이벤트
	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		final Intent i = new Intent(getActivity(), ChatListBackgroundActivity.class);
		Cursor cursor = (Cursor)mAdapter.getItem(position);
		i.putExtra(ChatListActivity.INTENT_ENABLE, cursor.getInt(cursor.getColumnIndexOrThrow(ViewTable.KEY_VIEW_ENABLE)));
		i.putExtra(ChatListActivity.INTENT_EXIST_VIEW, true);
		i.putExtra(ChatListActivity.INTENT_CLIENT_VIEW_MSB, cursor.getLong(cursor.getColumnIndex(ViewTable.KEY_CLIENT_VIEW_UUID_MSB)));
		i.putExtra(ChatListActivity.INTENT_CLIENT_VIEW_LSB, cursor.getLong(cursor.getColumnIndex(ViewTable.KEY_CLIENT_VIEW_UUID_LSB)));
		startActivity(i);
	}
			
	// 커서를 설정하고 데이터를 불러오는 부분
	public Loader<Cursor> onCreateLoader(int id, Bundle args) {
		Uri inboxListUri = ViewTable.CONTENT_URI;
		
		String select = "((" + ViewTable.KEY_CLIENT_VIEW_UUID_MSB + " NOTNULL) AND ("
				+ ViewTable.KEY_CLIENT_VIEW_UUID_LSB + " NOTNULL))";
		String sortOrder = ViewTable.KEY_MESSAGE_LAST_TIME + " COLLATE LOCALIZED DESC";
		return new CursorLoader(getActivity(), inboxListUri,
				ViewTable.PROJECTION, select, null, sortOrder);
	}

	public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
		mAdapter.swapCursor(data);
		/*mAdapter.registerDataSetObserver(new DataSetObserver() {
			public void onChanged() {
				super.onChanged();
				if(getActivity().getParent() instanceof HomeActivity)
				{
					Cursor c = mAdapter.getCursor();
					if( c != null && c.getCount() > 0 )
					{
						int count = 0;
						c.moveToFirst();
						for(int i = 0 ; i < c.getCount() ; i++)
						{
							count += c.getInt(c.getColumnIndexOrThrow(ViewTable.KEY_VIEW_UNREAD_COUNT));
							c.moveToNext();
						}
						
						((HomeActivity)getActivity().getParent()).updateInboxBadge(count);
					}
				}
			}
		});*/
	}
	

	public void onLoaderReset(Loader<Cursor> loader) {
		mAdapter.swapCursor(null);
	}
}